<?php

namespace Biz\model\store;

use Doraemon\pockets\datebase\BaseDbModel;

/**
 * 生成虚拟手机号记录表
 *
 */
class VirtualPhoneRecord extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'store_virtual_phone_record';

    const _SUB_TABLE = false;


    public static function create($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = time();
        }
        self::insert($data);
        return self::id();
    }

    public static function updateInfo($id, $data)
    {
        if (isset($data['edittime'])) {
            $data['edittime'] = time();
        }
        return self::update($data, [
            'id' => $id,
        ]);
    }

    public static function getOneById($id){
        return self::get('*',['id'=>$id]);
    }

    public static function getByCondition($condition)
    {
        return self::get('*', $condition);
    }

    /**
     * 根据过期时间倒序获取门店和用户最近的一次绑定记
     * @param $user_phone
     * @param $official_phone
     * @param $direction
     * @return array|bool
     */
    public static function getRecordInfo($user_phone, $official_phone, $direction)
    {
        return self::select('*', [
            "user_phone" => $user_phone,
            "official_phone" => $official_phone,
            "direction" => $direction,
            'ORDER' => [
                'virtual_expire_time' => 'DESC',
            ],
            'LIMIT' => 1,
        ]);
    }


    public static function listByCondition($condition)
    {
        return self::select('*', $condition);
    }

    /**
     * 根据绑定关系ID， 获取记录（用户user_uid, 门店store_id, 绑定关系id）
     *
     * @param array $subIdArr 绑定关系ID
     * @return array  [用户user_uid, 门店store_id, 绑定关系id]
     */
    public static function getInfoBySubid($subIdArr)
    {
        return self::select(['user_uid', 'store_id', 'subs_id'], [
            'subs_id' => $subIdArr
        ]);
    }

    /**
     * 根据绑定关系ID数组， 获取[真实手机号， 虚拟号码]
     *
     * @param array $subIdArr 绑定关系ID
     * @return array  真实号码
     */
    public static function getPhoneArrBySubIdS($subIdArr)
    {
        return self::select(['user_phone', 'virtual_phone','official_phone', 'subs_id'], [
            'subs_id' => $subIdArr
        ]);
    }
}
